//
//  SPMIneCollectionView.m
//  ShopProject
//
//  Created by kuangbiao on 2018/11/27.
//  Copyright © 2018 KB. All rights reserved.
//

#import "SPMIneCollectionView.h"


@interface SPMIneCollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
//列表
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation SPMIneCollectionView

- (instancetype)init{
    if(self = [super init]){
        [self initView];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if(self = [super initWithCoder:aDecoder]){
        [self initView];
    }
    return self;
}

- (void)initView{

    [self addSubview:self.collectionView];
}

- (instancetype)initWithFrame:(CGRect)frame{
    if([super initWithFrame:frame]){
        [self initView];
    }
    return self;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SPMineCollectionViewCell *cell = (SPMineCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"SPMineCollectionViewCell" forIndexPath:indexPath];
//    cell.btnBlock = ^(){
//        if([_delegate respondsToSelector:@selector(CollectionViewDidSelectedWithIndexpathItem:)]){
//            [_delegate CollectionViewDidSelectedWithIndexpathItem:indexPath.item];
//        }
//    };
    return  cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if([_delegate respondsToSelector:@selector(CollectionViewDidSelectedWithIndexpathItem:)]){
        [_delegate CollectionViewDidSelectedWithIndexpathItem:indexPath.item];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(collectionView.size.width/4, 100);
}
-(CGFloat )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

-(CGFloat )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

#pragma amrk - 懒加载
- (UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        //设置headerView的尺寸大小
        //        layout.headerReferenceSize = CGSizeMake(kScreenWidth, 144);
        //        layout.footerReferenceSize = CGSizeMake(kScreenWidth, 100);
        //2.初始化collectionView
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
        [_collectionView registerNib:[UINib nibWithNibName:@"SPMineCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"SPMineCollectionViewCell"];
        //4.设置代理
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

@end
